/**
 * Copyright (c) iwindplus Technologies Co., Ltd.2024-2030, All rights reserved.
 */

package com.iwindplus.auth.server.handler;

import com.iwindplus.boot.domain.vo.ResultVO;
import com.iwindplus.boot.web.domain.property.GlobalErrorProperty;
import com.iwindplus.boot.web.domain.property.ResponseBodyProperty;
import com.iwindplus.boot.web.mvc.handler.GlobalErrorHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

/**
 * 未认证处理.
 *
 * @author zengdegui
 * @since 2024/05/22 22:22
 */
@Slf4j
public record UnAuthenticationEntryPoint(GlobalErrorProperty globalErrorProperty, ResponseBodyProperty responseBodyProperty) implements
    AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
        throws IOException, ServletException {
        log.info("UnAuthenticationEntryPoint={}", authException);

        // 认证失败，401
        GlobalErrorHandler.responseData(response, this.globalErrorProperty, this.responseBodyProperty,
            HttpStatus.UNAUTHORIZED, ResultVO.error(HttpStatus.UNAUTHORIZED));
    }
}